package model.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import resources.util.HibernateUtil;
import model.dao.login.HibernateUsuarioDAO;
import model.interfaces.DAOFactory;
import model.interfaces.login.UsuarioDAO;

public class HibernateDAOFactory extends DAOFactory {

	private Session session;
	private Transaction tx;
	
	public HibernateDAOFactory() {
		this.session = HibernateUtil.getSession();
	}
	
	@Override
	public void iniciarTransacao() {
		this.tx = this.session.beginTransaction();
	}

	@Override
	public void cancelarTransacao() {
		this.tx.rollback();
		this.tx = null;		
	}

	@Override
	public void encerrar() {
		if (this.tx != null) {
			this.tx.commit();
		}
		this.session.close();		
	}
	
	@Override
	public UsuarioDAO getUsuarioDAO() {
		return new HibernateUsuarioDAO(this.session);
	}

	

}
